<?php

// +-----------------------------------------------------------------------
// | @Copyright (c) 2012 http://t00ls.net.
// +-----------------------------------------------------------------------
// | @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-----------------------------------------------------------------------
// | @author: lenush <jnicklasj@gmail.com> qq:707207845
// +-----------------------------------------------------------------------

class PsybaseAction extends CommonAction {

  function _filter(&$map){
    if(!empty($_POST['name'])) {
      $map['title'] = array('like',"%".$_POST['name']."%");
	  $map['status'] = 1;
    }
  }

  function insert() {
    $name = $this->getActionName();
    $model = D($name);
    if (false === $model->create()) {
      $this->error($model->getError());
    }
    $option = '';
    $score ='' ;
    if(!empty($_REQUEST['option1'])){
      $option .= addslashes(trim($_REQUEST['option1'])).'|||';	
      $scores .= intval($_REQUEST['score1']).'|||';	
    }
    if(!empty($_REQUEST['option2'])){
      $option .= addslashes(trim($_REQUEST['option2'])).'|||';	
      $scores .= intval($_REQUEST['score2']).'|||';	
    }
    if(!empty($_REQUEST['option3'])){
      $option .= addslashes(trim($_REQUEST['option3'])).'|||';	
      $scores .= intval($_REQUEST['score3']).'|||';	
    }
    if(!empty($_REQUEST['option4'])){
      $option .= addslashes(trim($_REQUEST['option4'])).'|||';	
      $scores .= intval($_REQUEST['score4']).'|||';	
    }
    //保存当前数据对象
    $data = array(
        'title' => addslashes(trim($_REQUEST['title'])),
        'option' => $option,
        'score' => $scores,
		'createat' => time(),
        );
    $list = $model->add($data);
    if ($list !== false) { //保存成功
      $this->success('新增成功!',cookie('_currentUrl_'));
    } else {
      //失败提示
      $this->error('新增失败!');
    }
  }

  function edit() {
    $name = $this->getActionName();
    $model = M($name);
    $id = $_REQUEST [$model->getPk()];
    $vo = $model->getById($id);
    $optionArr = explode('|||',$vo['option']);
    unset($optionArr[4]);
    $scoreArr = explode('|||',$vo['score']);
    unset($scoreArr[4]);
    $vo['option'] = $optionArr;
    $vo['score'] = $scoreArr;
    $this->assign('vo', $vo);
    $this->display();
  }

  public function update(){
    $name = $this->getActionName();
    $model = D($name);
    if (false === $model->create()) {
      $this->error($model->getError());
    }
    $option = '';
    $answer ='' ;
	  if(!empty($_REQUEST['option1'])){
		$option .= addslashes(trim($_REQUEST['option1'])).'|||';	
	  }
	  if(!empty($_REQUEST['option2'])){
		$option .= addslashes(trim($_REQUEST['option2'])).'|||';	
	  }
	  if(!empty($_REQUEST['option3'])){
		$option .= addslashes(trim($_REQUEST['option3'])).'|||';	
	  }
	  if(!empty($_REQUEST['option4'])){
		$option .= addslashes(trim($_REQUEST['option4'])).'|||';	
	  }
	  $answer = $_REQUEST['answer'];
    //保存当前数据对象
    $data = array(
        'id' => $_REQUEST['id'],
        'type' => $_REQUEST['type'],
        'title' => addslashes(trim($_REQUEST['title'])),
        'option' => $option,
        'answer' => $answer,
        );
    $list = $model->save($data);
    if ($list !== false) { //保存成功
      $this->success('编辑成功!',cookie('_currentUrl_'));
    } else {
      //失败提示
      $this->error('编辑失败!');
    }
  }

}
